package org.battery.controller;

import org.battery.entity.Page;
import org.battery.entity.Produce;
import org.battery.service.ProduceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 1t on 2017-08-16.
 */
@Controller
public class ProduceController extends BaseController{

    @Resource(name = "produceService")
    public ProduceService produceService;

    @RequestMapping(value = "/searchProduceTime")
    @ResponseBody
    public Map<String, Object> searchProduceTime() {
        Map<String, Object> map = new HashMap<String, Object>();
        Produce produce = new Produce();

        List<Produce> produceTime = null;
        try {
            produceTime = produceService.getProduceTime();
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("produceTimeList", produceTime);
        produce = null;
        return map;
    }

    @RequestMapping(value = "/searchProduceDay")
    @ResponseBody
    public Map<String, Object> searchProduceDay( Produce produce) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Produce> produceDay = null;
        try {
            produceDay = produceService.getProduceDay(produce);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("produceDayList", produceDay);
        produce = null;
        return map;
    }

    @RequestMapping(value = "/searchProduceName")
    @ResponseBody
    public Map<String, Object> searchProduceName(Produce produce) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Produce> produceName = null;
        try {
            produceName = produceService.getProduceName(produce);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("produceNameList", produceName);
        produce = null;
        return map;
    }

    @RequestMapping(value = "/searchProduceAll")
    @ResponseBody
    public Map<String, Object> searchProduceAll(Produce produce, String page, String rows) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page pageQuery = getPage();
        pageQuery.setCurrentPage(Integer.parseInt(page));
        pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
        pageQuery.setShowCount(Integer.parseInt(rows));

        List<Produce> produceListAll = null;
        try {
            produceListAll = produceService.getProduce(produce, pageQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("totalCnt", pageQuery.getTotalResult());
        map.put("totalPage", pageQuery.getTotalPage());
        map.put("produceListAll", produceListAll);
        produce = null;
        return map;
    }

}
